using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;
using FileFactory;

public partial class GonzalesUtilitiesExchangeRateMaster : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected DataObj CurPageDataObj = new DataObj();
    protected string UpdateDate = string.Empty;

    #region Properties
    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState["ResultTable"];
        }

        set
        {
            ViewState["ResultTable"] = value;
        }
    }
    #endregion

    #region GridViewHeader
    protected ArrayList GridViewHeader
    {
        get
        {
            return (ArrayList)ViewState["GridViewHeader"];
        }

        set
        {
            ViewState["GridViewHeader"] = value;
        }
    }
    #endregion

    #region ErrorMessage
    protected string ErrorMessage
    {
        get
        {
            return ViewState["ErrorMessage"].ToString();
        }

        set
        {
            ViewState["ErrorMessage"] = value;
        }
    }
    #endregion

    #region Moded
    protected string Moded
    {
        get
        {
            return ViewState["MOD"].ToString();
        }

        set
        {
            ViewState["MOD"] = value;
        }
    }
    #endregion

    #region UpdatedMessage
    protected string UpdatedMessage
    {
        get
        {
            return ViewState["UpdatedMessage"].ToString();
        }

        set
        {
            ViewState["UpdatedMessage"] = value;
        }
    }
    #endregion

    #region DeletionFailedMessage
    protected string DeletionFailedMessage
    {
        get
        {
            return ViewState["DeletionFailedMessage"].ToString();
        }

        set
        {
            ViewState["DeletionFailedMessage"] = value;
        }
    }
    #endregion

    #region EditingFailedMessage
    protected string EditingFailedMessage
    {
        get
        {
            return ViewState["EditingFailedMessage"].ToString();
        }

        set
        {
            ViewState["EditingFailedMessage"] = value;
        }
    }
    #endregion

    #region SAMessage
    protected string SAMessage
    {
        get
        {
            return ViewState["SAMessage"].ToString();
        }

        set
        {
            ViewState["SAMessage"] = value;
        }
    }
    #endregion

    #region DuplicateSAMessage
    protected string DuplicateSAMessage
    {
        get
        {
            return ViewState["DuplicateSAMessage"].ToString();
        }

        set
        {
            ViewState["DuplicateSAMessage"] = value;
        }
    }
    #endregion

    #region EditLink
    protected string EditLink
    {
        get
        {
            return ViewState["EditLink"].ToString();
        }

        set
        {
            ViewState["EditLink"] = value;
        }
    }
    #endregion

    #region DeleteLink
    protected string DeleteLink
    {
        get
        {
            return ViewState["DeleteLink"].ToString();
        }

        set
        {
            ViewState["DeleteLink"] = value;
        }
    }
    #endregion
    #endregion

    #region Events

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if(this.DropDownList_UpdateDate.SelectedValue != null)
            this.UpdateDate = this.DropDownList_UpdateDate.SelectedValue;

        this.SessionCheck();

        this.DataDelete();

        if (!IsPostBack)
        {
            this.SwitchLanguage();
            this.PageComponentSetup();
            this.TextBox_UpdateDate.Text = SwissArmy.DateConvert(DateTime.Today, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat, "/", false).ToString();
            Moded = "Add";
            this.DataEdit();
            this.LoadDataBinding();
        }
    }
    #endregion

    #region Button_Submit_Click
    protected void Button_Submit_Click(object sender, EventArgs e)
    {
        if (this.DataValidate())
        {
            using (ExchangeRateManager CurExRateManager = new ExchangeRateManager(this.CurrentPrivateSession))
            {
                bool smooth = false;
                DateTime dt = SwissArmy.DateConvert(TextBox_UpdateDate.Text.Trim(), this.CurrentPrivateSession.DateFormatInUse);
                if (Moded.CompareTo("Update") == 0)
                    smooth = CurExRateManager.UpdateExRateBySourceTarget(
                        DropDownList_SourceCurr.SelectedValue,
                        DropDownList_TargetCurr.SelectedValue,
                        TextBox_ExRate.Text.Trim(),
                        dt.ToString("MM/dd/yyyy"), dt.Month, dt.Year);
                else
                    smooth = CurExRateManager.AddExchangeRate(
                        DropDownList_SourceCurr.SelectedValue,
                        DropDownList_TargetCurr.SelectedValue,
                        TextBox_ExRate.Text.Trim(),
                        dt.ToString("MM/dd/yyyy"));

                if(smooth)
                {
                    // log down the history
                    this.CurLogManager.LogMessage("[GonzalesUtilitiesExchangeRateMaster] : Source Currency (" + this.DropDownList_SourceCurr.SelectedValue + ", Target Currency: " + this.DropDownList_TargetCurr.SelectedValue + ", Exchange Rate: " + this.TextBox_ExRate.Text.Trim() + ", Update Date: " + this.TextBox_UpdateDate.Text + ") added ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);

                    this.ShowMessage(this.UpdatedMessage);

                    string lsYear = dt.Year.ToString();
                    string lsMonth = dt.Month.ToString();
                    string lsYearMonth = lsYear + "." + (lsMonth.Length == 2 ? lsMonth : "0" + lsMonth);

                    if (this.DropDownList_UpdateDate.Items.FindByValue(lsYearMonth) != null)
                        this.DropDownList_UpdateDate.Items.FindByValue(lsYearMonth).Selected = true;

                    this.LoadData();
                    if (this.ResultTable != null)
                    {
                        DataView ResultView = new DataView(this.ResultTable);
                        ResultView.Sort = this.CurPageDataObj.SourceCurrency + " ASC";
                        this.BindData(ResultView);
                    }
                    this.ResetData(); // reset data

                }
                else
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesUtilitiesExchangeRateMaster.Button_Submit_Click] : Failed at CurExRateManager.AddExchangeRate('" + DropDownList_SourceCurr.SelectedValue + "', '" + DropDownList_TargetCurr.SelectedValue + "', '" + TextBox_ExRate.Text.Trim() + "', '" + TextBox_UpdateDate.Text.Trim() + "') : " + CurExRateManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage(this.ErrorMessage + LogID);
                } 
            }
        }
    }
    #endregion

    #region Button_Import_Click
    protected void Button_Import_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.FileUploadSource.PostedFile != null && this.FileUploadSource.PostedFile.FileName.Length > 0)
            {
                FileFactory.FileFactory ff = new FileFactory.FileFactory(this.CurrentPrivateSession);
                ff.UpdateDate = SwissArmy.DateConvert(TextBox_UpdateDate.Text.Trim(), this.CurrentPrivateSession.DateFormatInUse);

                if(ff.StartWatching(this.FileUploadSource.PostedFile.InputStream, "R", ""))
                    Response.Redirect("GonzalesUtilitiesExchangeRateMaster.aspx");
                else
                    this.ShowErrorMessage(ff.LoggedMessages[0].ToString());
            }
            else
            {
                this.ShowErrorMessage("Invalid File");
            }
        }
        catch (Exception)
        {
            // do nothing
        }
    }
    #endregion

    #region OnRowCreated
    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            for (int i = 0; i < this.GridViewHeader.Count; i++)
            {
                cells[i].Text = this.GridViewHeader[i].ToString();
            }
        }
    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {
        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }
    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        this.TextBox_UpdateDate.Attributes["onclick"] = "popUpCalendar(this, this, '" + SwissArmy.DateFormatForDisplay(this.CurrentPrivateSession.DateFormatInUse, "/") + "');";

        using (ExchangeRateManager CurExRateManager = new ExchangeRateManager(this.CurrentPrivateSession))
        {
            MasterBaseCollection ResultList = CurExRateManager.GetAllCurrency();
            if (ResultList != null)
            {
                this.DropDownList_TargetFilter.Items.Add(new ListItem("", ""));
                for (int x = 0; x < ResultList.Count; x++)
                {
                    this.DropDownList_SourceCurr.Items.Add(new ListItem(ResultList[x].DisplayName, ResultList[x].InternalID));
                    this.DropDownList_TargetCurr.Items.Add(new ListItem(ResultList[x].DisplayName, ResultList[x].InternalID));
                    this.DropDownList_TargetFilter.Items.Add(new ListItem(ResultList[x].DisplayName, ResultList[x].InternalID));
                }
            }
            else
            {
                // display error
                this.CurLogManager.LogError("[GonzalesUtilitiesExchangeRateMaster.PageComponentSetup] : Failed on CurExRateManager.GetAllCurrency() : " + CurExRateManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
            }

            MasterBaseCollection UpdateDateList = CurExRateManager.GetDistinctExRateUpdateDate();
            if (UpdateDateList != null)
            {
                for (int x = 0; x < UpdateDateList.Count; x++)
                {
                    this.DropDownList_UpdateDate.Items.Add(new ListItem(UpdateDateList[x].DisplayName, UpdateDateList[x].InternalID));
                }

            }
            else
            {
                // display error
                this.CurLogManager.LogError("[GonzalesUtilitiesExchangeRateMaster.PageComponentSetup] : Failed on CurExRateManager.GetDistinctExRateUpdateDate() : " + CurExRateManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
            }
        }
        //this.DropDownList_SourceCurr.Items.FindByValue(this.CurrentPrivateSession.CurrentLoggedOnUser.DefaultCurrency.InternalID).Selected = true;

        this.DropDownList_TargetCurr.Items.FindByValue(this.CurrentPrivateSession.CurrentLoggedOnUser.DefaultCurrency.InternalID).Selected = true;
        this.DropDownList_TargetFilter.Items.FindByValue(this.CurrentPrivateSession.CurrentLoggedOnUser.DefaultCurrency.InternalID).Selected = true; 
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.lbl_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.lbl_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.lbl_AddNew.Text = CurLanguageFactory.DisplayValue("AddNew");

                this.lbl_SourceCurrency.Text = CurLanguageFactory.DisplayValue("SourceCurrency");
                this.lbl_TargetCurrency.Text = CurLanguageFactory.DisplayValue("TargetCurrency");
                this.lbl_ExchangeRate.Text = CurLanguageFactory.DisplayValue("ExchangeRate");
                this.lbl_DateUpdate.Text = CurLanguageFactory.DisplayValue("DateUpdated");
                
                this.Button_Submit.Text = CurLanguageFactory.DisplayValue("Submit");
                // Create GridView
                this.GridViewHeader = new ArrayList();
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_SourceCurrency"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_TargetCurrency"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_ExchangeRate"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_DateUpdated"));

                this.EditLink = CurLanguageFactory.DisplayValue("EditLink");
                this.DeleteLink = CurLanguageFactory.DisplayValue("DeleteLink");
                // default
                this.ErrorMessage = CurLanguageFactory.DisplayValue("ErrorMessage");
                this.UpdatedMessage = CurLanguageFactory.DisplayValue("UpdatedMessage");
                this.DeletionFailedMessage = CurLanguageFactory.DisplayValue("DeletionFailedMessage");
                this.SAMessage = CurLanguageFactory.DisplayValue("SAMessage");
                this.DuplicateSAMessage = CurLanguageFactory.DisplayValue("DuplicateSAMessage");
                this.EditingFailedMessage = CurLanguageFactory.DisplayValue("EditingFailedMessage");
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesUtilitiesExchangeRateMaster.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region ShowErrorMessage
    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "ErrorBox";
    }
    #endregion

    #region ShowMessage
    protected void ShowMessage(string Message)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = Message;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "MessageBox";
    }
    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        using (ExchangeRateManager CurExRateManager = new ExchangeRateManager(this.CurrentPrivateSession))
        {
            MasterBaseCollection ExRateCollection = null;
            if (DropDownList_UpdateDate.SelectedValue.CompareTo("") == 0)
                ExRateCollection = CurExRateManager.GetExchangeRateByDate(DateTime.Today, DropDownList_TargetFilter.SelectedValue);
            else
            {
                string[] yearmon = DropDownList_UpdateDate.SelectedValue.Split('.');
                ExRateCollection = CurExRateManager.GetExchangeRateByDate(DateTime.Parse(yearmon[1] + "/01/"+ yearmon[0]), DropDownList_TargetFilter.SelectedValue);
            }

            if (ExRateCollection != null)
            {
                this.ResultTable = new DataTable();
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.SourceCurrency, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.TargetCurrency, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.ExchangeRate, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.DateUpdated, Type.GetType("System.String")));
                    
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.EditLink, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.DeleteLink, Type.GetType("System.String")));

                for (int x = 0; x < ExRateCollection.Count; x++)
                {
                    DataRow ResultRow = this.ResultTable.NewRow();

                    ResultRow[this.CurPageDataObj.SourceCurrency] = ((ExchangeRate)ExRateCollection[x]).SourceCurrency;
                    ResultRow[this.CurPageDataObj.TargetCurrency] = ((ExchangeRate)ExRateCollection[x]).TargetCurrency;
                    ResultRow[this.CurPageDataObj.ExchangeRate] = ((ExchangeRate)ExRateCollection[x]).ExRate;
                    ResultRow[this.CurPageDataObj.DateUpdated] = SwissArmy.DateConvert(((ExchangeRate)ExRateCollection[x]).UpdateDate, TypeOfDateFormat.MMDDYYYY, "/", false);

                    ResultRow[this.CurPageDataObj.EditLink] = "<a class='GridViewLink' href='GonzalesUtilitiesExchangeRateMaster.aspx?EDIT=TRUE&id=" + ((ExchangeRate)ExRateCollection[x]).SourceCurrency + ";" + ((ExchangeRate)ExRateCollection[x]).TargetCurrency + ";" + SwissArmy.DateConvert(((ExchangeRate)ExRateCollection[x]).UpdateDate, this.CurrentPrivateSession.DateFormatInUse, "/", false) + "' >" + this.EditLink + "</a>";
                    ResultRow[this.CurPageDataObj.DeleteLink] = "<a class='GridViewLink' href='GonzalesUtilitiesExchangeRateMaster.aspx?DELETE=TRUE&id=" + ((ExchangeRate)ExRateCollection[x]).SourceCurrency + ";" + ((ExchangeRate)ExRateCollection[x]).TargetCurrency + ";" + SwissArmy.DateConvert(((ExchangeRate)ExRateCollection[x]).UpdateDate, TypeOfDateFormat.MMDDYYYY, "/", false) + "' onclick=\"return confirm('This action will not able to be undone, continue?');\">" + this.DeleteLink + "</a>";

                    this.ResultTable.Rows.Add(ResultRow);
                }
            }
            else
            {
                this.CurLogManager.LogError("[GonzalesUtilitiesExchangeRateMaster.LoadData] : Failed at : " + CurExRateManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
            }
        }
        
    }
    #endregion    

    #region LoadDataBinding
    protected void LoadDataBinding()
    {
        this.LoadData();
        if (this.ResultTable != null)
        {
            DataView ResultView = new DataView(this.ResultTable);
            ResultView.Sort = this.CurPageDataObj.SourceCurrency + " ASC";

            this.BindData(ResultView);
        }
    }
    #endregion

    #region DataDelete
    protected void DataDelete()
    {
        bool smooth = true;
        if (Request.QueryString["id"] == null) return;
        string[] lsArr = Request.QueryString["id"].ToString().Split(';');

        if (Request.QueryString["id"] != null && lsArr.Length == 3 &&
            Request.QueryString["DELETE"] != null && Request.QueryString["DELETE"].CompareTo("TRUE") == 0)
        {
            using (ExchangeRateManager CurExRateManager = new ExchangeRateManager(this.CurrentPrivateSession))
            {
                if (!(smooth = CurExRateManager.RemoveExRateBySourceTargetDate(lsArr[0], lsArr[1], lsArr[2])))
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesUtilitiesExchangeRateMaster.DataDelete] : Failed at CurExRateManager.RemoveExRateBySourceTargetDate(Request.QueryString['" + Request.QueryString["id"] + "']) : " + CurExRateManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage(this.DeletionFailedMessage + LogID);
                }
            }

            if (smooth)
            {
                Response.Redirect("GonzalesUtilitiesExchangeRateMaster.aspx");
            }
        }
    }

    #endregion

    #region DataEdit
    protected void DataEdit()
    {
        if (Request.QueryString["id"] == null) return;
        string[] lsArr = Request.QueryString["id"].ToString().Split(';');

        if (Request.QueryString["id"] != null && lsArr.Length == 3 &&
            Request.QueryString["EDIT"] != null && Request.QueryString["EDIT"].CompareTo("TRUE") == 0)
        {
            using (ExchangeRateManager CurExRateManager = new ExchangeRateManager(this.CurrentPrivateSession))
            {
                DateTime dt = SwissArmy.DateConvert(lsArr[2], this.CurrentPrivateSession.DateFormatInUse);
                MasterBaseCollection ExRateCollection = CurExRateManager.GetExchangeRateBySourceTargetDate(lsArr[0], lsArr[1], dt.ToString("yyyyMMdd"));
                if (ExRateCollection != null && ExRateCollection.Count == 1)
                {
                    // Set values ------
                    this.DropDownList_SourceCurr.Items.FindByValue(this.DropDownList_SourceCurr.SelectedValue).Selected = false; // de-selected default value
                    if(this.DropDownList_SourceCurr.Items.FindByValue(((ExchangeRate)ExRateCollection[0]).SourceCurrency) != null)
                        this.DropDownList_SourceCurr.Items.FindByValue(((ExchangeRate)ExRateCollection[0]).SourceCurrency).Selected = true; // selected value
                    if(this.DropDownList_TargetCurr.Items.FindByValue(this.DropDownList_TargetCurr.SelectedValue) != null)
                        this.DropDownList_TargetCurr.Items.FindByValue(this.DropDownList_TargetCurr.SelectedValue).Selected = false; // de-selected default value
                    if(this.DropDownList_TargetCurr.Items.FindByValue(((ExchangeRate)ExRateCollection[0]).TargetCurrency) != null)
                        this.DropDownList_TargetCurr.Items.FindByValue(((ExchangeRate)ExRateCollection[0]).TargetCurrency).Selected = true; // selected value

                    TextBox_ExRate.Text = ((ExchangeRate)ExRateCollection[0]).ExRate.ToString();
                    TextBox_UpdateDate.Text = SwissArmy.DateConvert( ((ExchangeRate)ExRateCollection[0]).UpdateDate, this.CurrentPrivateSession.DateFormatInUse, "/", false);

                    this.DropDownList_SourceCurr.Enabled = false;
                    this.DropDownList_TargetCurr.Enabled = false;
                    this.TextBox_UpdateDate.Enabled = false;
                    Moded = "Update";
                   
                    this.Button_Submit.Text = "Update";
                    string lsYear = ((ExchangeRate)ExRateCollection[0]).UpdateDate.Year.ToString();
                    string lsMonth = ((ExchangeRate)ExRateCollection[0]).UpdateDate.Month.ToString();
                    string lsYearMonth = lsYear + "." + (lsMonth.Length == 2 ? lsMonth : "0" + lsMonth);

                    if (this.DropDownList_UpdateDate.Items.FindByValue(lsYearMonth) != null)
                        this.DropDownList_UpdateDate.Items.FindByValue(lsYearMonth).Selected = true;
                }
                else
                {
                    // Editing error
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesUtilitiesExchangeRateMaster.DataEdit] : Failed at  CurExRateManager.GetProjectByEntityProjectID(Request.QueryString['" + Request.QueryString["id"] + "']) : " + CurExRateManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage(this.EditingFailedMessage + LogID);
                }
            }
        }
    }
    #endregion

    #region ResetData
    protected void ResetData()
    {
        TextBox_ExRate.Text = "1";
        this.TextBox_UpdateDate.Enabled = true;
        TextBox_UpdateDate.Text = SwissArmy.DateConvert(DateTime.Today, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat, "/", false).ToString();
        this.Moded = "Add";
        this.DropDownList_SourceCurr.Enabled = this.DropDownList_TargetCurr.Enabled = true;
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Button_Submit.Text = CurLanguageFactory.DisplayValue("Submit");
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesUtilitiesExchangeRateMaster.Button_Cancel_Click.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }
    }
    #endregion

    #region DataValidate
    protected bool DataValidate()
    {
        bool valid = true;
        StringBuilder ErrorMessages = new StringBuilder("<ul>");

        if (this.TextBox_ExRate.Text.Trim().Length == 0)
        {
            valid = false;
            ErrorMessages.AppendLine("<li>" + this.SAMessage + "</li>");
        }
        // check if having duplicate entry
        if (Moded.CompareTo("Add") == 0)
        {
            using (ExchangeRateManager CurExRateManager = new ExchangeRateManager(this.CurrentPrivateSession))
            {
                DateTime dtNew = SwissArmy.DateConvert(TextBox_UpdateDate.Text.Trim(), this.CurrentPrivateSession.DateFormatInUse);
                MasterBaseCollection ExRateCollection = CurExRateManager.GetExchangeRateBySourceTargetDate(this.DropDownList_SourceCurr.SelectedValue, this.DropDownList_TargetCurr.SelectedValue, dtNew.ToString("yyyyMMdd"));
                if (ExRateCollection != null && ExRateCollection.Count == 1)
                {
                    valid = false;
                    ErrorMessages.AppendLine("<li>" + this.DuplicateSAMessage + "</li>");
                }
            }
        }

        ErrorMessages.AppendLine("</ul>");

        if (!valid) this.ShowErrorMessage(ErrorMessages.ToString());

        return valid; 
    }
    #endregion

    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    #endregion

    #region Class: DataObj

    protected class DataObj
    {
        public readonly string SourceCurrency = "source_currency";
        public readonly string TargetCurrency = "target_currency";
        public readonly string ExchangeRate = "ex_rate";
        public readonly string DateUpdated = "update_date";
        
        public readonly string EditLink = "EditLink";
        public readonly string DeleteLink = "DeleteLink";
    }

    #endregion

    protected void DropDownList_UpdateDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadDataBinding();
    }
    protected void DropDownList_TargetFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadDataBinding();
    }
    protected void DropDownList_SortBy_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList_SortOrder_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button_Cancel_Click(object sender, EventArgs e)
    {
        ResetData();
    }
}
